import express from "express"
import path from "path"
import resret from "./public/commonPort/resret"
import {
    readFlie,
    writeFlie
} from "../fs_file包封装/node_modules/fs_file/index"

const staticFile = path.join(__dirname, "./public")
const app = express()

app.listen(3000, () => {
    console.log("sever had run with express as http//localhost:3000");
})
//处理静态资源
app.use(
    //静态资源请求
    express.static(staticFile)
    //匹配接口
)
//处理GET请求
app.get("/check", (req, res, next) => { //渲染
    var arr = JSON.parse(readFlie(path.join(__dirname, "./public/cook/json/json.json")))
    res.send(arr)
})
app.get("/remove", (req, res, next) => { //删除
    let {
        id
    } = req.query
    console.log(id);
    var arr = JSON.parse(readFlie(path.join(__dirname, "./public/cook/json/json.json")))
    arr.forEach((item, i) => {
        item.list.forEach((key, j) => {
            if (key["id"] === id) {
                arr[i].list[j] = {}
                writeFlie(path.join(__dirname, "./public/cook/json/json.json"), JSON.stringify(arr))
                res.send(arr)
            }
        })
    });
})
app.get("/amend", (req, res, next) => {
    var arr = JSON.parse(readFlie(path.join(__dirname, "./public/cook/json/json.json")))
    let {
        is,
        name,
        cs,
        num,
        day
    } = req.query
    console.log(req.query);
    let str = is
    let brr = str.split("-")
    let [i, j] = brr
    i *= 1
    j *= 1
    let b = arr[i]["list"][j]
    b["name"] = name
    b["class"] = cs
    b["num"] = num
    b["day"] = day
    arr[i]["list"][j] = b
    writeFlie(path.join(__dirname, "./public/cook/json/json.json"), JSON.stringify(arr))
    resret({
        res,
        text: "001"
    })
})
app.get("/sort", (req, res, next) => {
    let crr = JSON.parse(readFlie(path.join(__dirname, "./public/cook/json/json.json")))
    let {
        typehade,
        typeend
    } = req.query
    let list = []
    crr.forEach(item => {
        if (item["class"] === typehade || typehade === "全部") {
            item["list"].forEach(key => {
                if (key["name"].indexOf(typeend) != -1 || typeend === "") {
                    list.push(key)
                }
            })
        }
    })
    res.send(list)
})